package cn.tedu.ivos2.dict.service.impl;

import cn.tedu.ivos2.dict.mapper.DictMapper;
import cn.tedu.ivos2.dict.pojo.dto.DictQuery;
import cn.tedu.ivos2.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos2.dict.pojo.entity.Dict;
import cn.tedu.ivos2.dict.pojo.vo.DictVO;
import cn.tedu.ivos2.dict.service.DictService;
import cn.tedu.ivos2.dictoption.mapper.DictOptionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictOptionMapper  dictOptionMapper;
    public List<DictVO> search(DictQuery dictQuery) {
        return dictMapper.search(dictQuery);
    }

    @Override
    public void save(DictSaveParam dictSaveParam) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam, dict);
        if (dict.getId() == null) {
            dict.setCreateTime(new Date());
            dictMapper.save(dict);
        } else {
            dict.setUpdateTime(new Date());
            dictMapper.change(dict);
        }
    }

    @Override
    public void delete(Long id) {
        dictOptionMapper.deleteByDictId(id);
        dictMapper.delete(id);
    }
}
